package be.ipl.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import be.ipl.domaine.Jeton;
import be.ipl.ucc.GestionParties;
import be.ipl.ucc.GestionTableau;

/**
 * Servlet implementation class LancerUnePartie
 */
@WebServlet(name = "lancerPartie.html")
public class LancerUnePartie extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB
	private GestionParties gestionParties;
	@EJB
	private GestionTableau gestionTableau;


	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int partieId = 0;
		String login = "";

		HttpSession session = request.getSession();
		synchronized (session) {
			partieId = (int) session.getAttribute("valeurID");
			login = (String) session.getAttribute("login");
		}

		try {
			if (gestionParties.partieEstEnCours(partieId)) {
				int de = gestionParties.getDe(partieId);
				request.setAttribute("de", de);

				List<Jeton> listejetons = new ArrayList<Jeton>();
				listejetons = gestionParties.donnerTroisJetons(partieId);

				request.setAttribute("listejetons", listejetons);
				
				List<Integer> listePositionsPossibles = new ArrayList<Integer>();
				listePositionsPossibles = gestionTableau.casesPossibles(partieId, de, login);
				
				request.setAttribute("listepositions", listePositionsPossibles);
				RequestDispatcher rd = getServletContext().getNamedDispatcher(
						"creer.html");
				rd.forward(request, response);
				return;
			} else {
				if(gestionParties.commencerPartie(partieId)){
					
					int de = gestionParties.getDe(partieId);
					
					request.setAttribute("de", de);

					List<Jeton> listejetons = new ArrayList<Jeton>();
					listejetons = gestionParties.donnerTroisJetons(partieId);
					
					List<Integer> listePositionsPossibles = new ArrayList<Integer>();
					listePositionsPossibles = gestionTableau.casesPossibles(partieId, de, login);
					
					request.setAttribute("listejetons", listejetons);
					request.setAttribute("listepositions", listePositionsPossibles);

					RequestDispatcher rd = getServletContext().getNamedDispatcher(
							"creer.html");
					rd.forward(request, response);
					return;
				}else{
					System.out.println("erreur supprimer partie");
					gestionParties.supprimerPartie(partieId);
					RequestDispatcher rd = getServletContext().getNamedDispatcher(
							"accueil.html");
					rd.forward(request, response);
					return;
				}
			}

		} catch (Exception e) {
			request.setAttribute("message",
					"Probleme au niveau serveur : " + e.getMessage());
			RequestDispatcher rd = getServletContext().getNamedDispatcher(
					"ErreurParam");
			rd.forward(request, response);
			return;
		}
	}

}
